package com.edu.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.boot.entity.CommunityComment;
import com.edu.boot.service.CommunityCommentService;
import com.edu.boot.mapper.CommunityCommentMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author Janson
* @description 针对表【community_comment(评论数据表)】的数据库操作Service实现
* @createDate 2025-04-12 22:39:33
*/
@Service
public class CommunityCommentServiceImpl extends ServiceImpl<CommunityCommentMapper, CommunityComment>
    implements CommunityCommentService{
    //获取全部子评论
    private void findAllChildCommentIds(Integer parentId, List<Integer> result) {
        List<CommunityComment> childComments = this.list(
                new LambdaQueryWrapper<CommunityComment>()
                        .eq(CommunityComment::getParentId, parentId)
        );
        
        for (CommunityComment child : childComments) {
            result.add(child.getId());
            findAllChildCommentIds(child.getId(), result); // 递归处理孙子评论
        }
    }
    public void deleteComment(Integer id) {
        // 1. 获取所有相关评论（该评论及其所有子评论）
        List<Integer> idsToDelete = new ArrayList<>();
        findAllChildCommentIds(id, idsToDelete);
    
        // 2. 加入当前节点自身 ID
        idsToDelete.add(id);
    
        // 3. 批量删除
        this.removeByIds(idsToDelete);
    }

}




